//
//  CapsuleListViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/17.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "CapsuleListViewController.h"
#import "CityModel.h"
#import "CapCell.h"
#import "DisCountWebView.h"

@interface CapsuleListViewController ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic, strong) NSMutableArray *listArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSDictionary *dataDic;
@property (nonatomic, assign) NSInteger page;

@end

@implementation CapsuleListViewController

// 视图的出现将tabbar隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)creatData {
    self.page++;
    NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/mguide_detail?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863151025525517&track_app_version=6.7.1&track_app_channel=baidu&track_device_info=mx3&track_os=Android4.4.4&app_installtime=1444613810999&lat=38.882887&lon=121.545518&id=%@&page=%ld&count=10",self.listStr,self.page];
    NSLog(@"%@",self.listStr);
    [ZSMAFNetWork geturl:str body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        self.dataDic = [NSDictionary dictionaryWithDictionary:result[@"data"]];
        NSArray *arr = result[@"data"][@"pois"];
        for (NSDictionary *dic in arr) {
            CityModel *cityList = [[CityModel alloc]initWithDic:dic];
            [self.listArr addObject:cityList];
        }
        [self.tableView reloadData];
        self.hud.hidden = YES;
        [self.tableView.footer endRefreshing];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
    
    self.listArr = [NSMutableArray array];
    [self creatData];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 667) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;


    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate  = self;
    self.tableView.tableFooterView = [UIView new];

    self.tableView.rowHeight = HEIGHT * 300;

    self.hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self.hud hide:YES afterDelay:2];
    // Do any additional setup after loading the view.
    
    // 左上角返回图标
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];

    [self createMJRefresh];
}
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, self.view.bounds.size.height/2, WIDTH*375, 30)];
    label.text = @"! 抱歉,没有内容";
    label.textAlignment = NSTextAlignmentCenter ;
    label.textColor = [UIColor redColor];
    
    return label;
    
}
// 返回
- (void)leftAction:(UIBarButtonItem *)barButton {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createMJRefresh {
    // 上拉加载
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(creatData)];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.listArr.count+1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row != 0) {
        CityModel *city = self.listArr[indexPath.row-1];
        NSMutableDictionary *fontDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12],NSFontAttributeName, nil];
        CGRect rect = [city.descriPtion boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
        return HEIGHT * 250 + rect.size.height;
    }
    return HEIGHT * 300;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    // 第一个cell是详贴
    if (indexPath.row >= 1) {
        static NSString *reuse = @"cell";
        CapCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        
        if (!cell) {
            cell = [[CapCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        }
          cell.selectionStyle = UITableViewCellSelectionStyleNone;
        CityModel *city = self.listArr[indexPath.row-1];
        [cell.topImage sd_setImageWithURL:[NSURL URLWithString:city.photo] placeholderImage:[UIImage imageNamed:@"F"]];
        cell.nameLabel.text = city.chinesename;
        // 无半星情况
        if (city.recommandstar%2 == 0) {
            for (NSInteger i = 0; i < city.recommandstar/2; i++) {
                switch (i) {
                    case 0:
                        [cell.imageViewOne setImage:[UIImage imageNamed:@"iconfont-zuixin-4"]];
                        
                        break;
                    case 1:
                         [cell.imageViewTwo setImage:[UIImage imageNamed:@"iconfont-zuixin-4"]];
                        break;
                    case 2:
                        [cell.imageViewThree setImage:[UIImage imageNamed:@"iconfont-zuixin-4"]];
                        break;
                    case 3:
                        [cell.imageViewFour setImage:[UIImage imageNamed:@"iconfont-zuixin-4"]];
                        break;
                    case 4:
                        [cell.imageViewFive setImage:[UIImage imageNamed:@"iconfont-zuixin-4"]];
                        break;
                    default:
                        break;
                }
            }
            
            for (NSInteger i = 0; i < 5-city.recommandstar/2; i++) {
                switch (i) {
                    case 0:
                        [cell.imageViewFive setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                        break;
                    case 1:
                        [cell.imageViewFour setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                        break;
                    case 2:
                        [cell.imageViewThree setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                        break;
                    case 3:
                        [cell.imageViewTwo setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                        break;
                    case 4:
                        [cell.imageViewOne setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                        break;
                        
                    default:
                        break;
                }
            }
// 有半星的情况
        }else if (city.recommandstar%2 == 1) {
            for (NSInteger i = 0; i < city.recommandstar/2; i++) {
                switch (i) {
                    case 0:
                        [cell.imageViewOne setImage:[UIImage imageNamed:@"iconfont-zuixin-4"]];
                        [cell.imageViewTwo setImage:[UIImage imageNamed:@"iconfont-pingfenxingxingbankesvg-3"]];
                        
                        break;
                    case 1:
                        [cell.imageViewTwo setImage:[UIImage imageNamed:@"iconfont-zuixin-4"]];
                        [cell.imageViewThree setImage:[UIImage imageNamed:@"iconfont-pingfenxingxingbankesvg-3"]];
                        break;
                    case 2:
                        [cell.imageViewThree setImage:[UIImage imageNamed:@"iconfont-zuixin-4"]];
                        [cell.imageViewFour setImage:[UIImage imageNamed:@"iconfont-pingfenxingxingbankesvg-3"]];
                        break;
                    case 3:
                        [cell.imageViewFour setImage:[UIImage imageNamed:@"iconfont-zuixin-4"]];
                        [cell.imageViewFive setImage:[UIImage imageNamed:@"iconfont-pingfenxingxingbankesvg-3"]];
                        break;
                        
                    default:
                        break;
                }

        }
            for (NSInteger i = 0; i < 5-city.recommandstar/2; i++) {
                switch (i) {
                   
                    case 1:
                        [cell.imageViewFive setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                        break;
                    case 2:
                        [cell.imageViewFour setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                        break;
                    case 3:
                        [cell.imageViewThree setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                        break;
                    case 4:
                        [cell.imageViewTwo setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                        break;
                        
                    default:
                        break;
                }
            }

        }
     
        
        // description是关键字
        cell.contentListLabel.text = city.descriPtion ;
        return cell;
   // 主贴
    }else if (indexPath.row == 0) {
        static NSString *reuse = @"pocell";
        CapCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        NSLog(@"!!!!!%ld",indexPath.row);
        if (!cell) {
            cell = [[CapCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        }
        [cell.topImage sd_setImageWithURL:[NSURL URLWithString:self.dataDic[@"photo"]] placeholderImage:[UIImage imageNamed:@"F"]];
        [cell.littleImage sd_setImageWithURL:[NSURL URLWithString:self.dataDic[@"avatar"]] placeholderImage:[UIImage imageNamed:@"F"]];
        cell.numLabel.text = self.dataDic[@"count"];
        cell.locaLabel.text = @"目的地";
        cell.userLabel.text = self.dataDic[@"username"];
        cell.titleLabel.text = self.dataDic[@"title"];
        // description是关键字
        cell.contentLabel.text = self.dataDic[@"description"] ;
        
        return cell;

    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row != 0) {
          CityModel *city = self.listArr[indexPath.row-1];
        NSString *body = [NSString stringWithFormat:@"client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863151025525517&track_app_version=6.7.1&track_app_channel=baidu&track_device_info=mx3&track_os=Android4.4.4&app_installtime=1444613810999&lat=38.883443&lon=121.544998&client_id=qyer_android&poi_id=%@&screensize=1800",city.mId];
        NSLog(@"我擦%@",body);
    
        NSDictionary *dic = @{@"Content-Type":@"application/x-www-form-urlencoded"};
        [ZSMAFNetWork posturl:@"http://open.qyer.com/qyer/footprint/poi_detail" body:body result:ZSMJSON requestStyle:ZSMRequestString headerFile:dic success:^(id result) {
          NSString *str =  result[@"data"][@"qyerurl"];
            
            DisCountWebView *web = [[DisCountWebView alloc]init];
            web.disCountStr = str;
            [self.navigationController pushViewController:web animated:YES];
        } failure:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
     
    }
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.tableView.backgroundColor = [UIColor blackColor];
    }
    else {
        self.tableView.backgroundColor = [UIColor whiteColor];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
